package edu.maping.easy.sequence;

import java.util.List;

import edu.maping.easy.editor.EasyMappingEditor;
import edu.maping.easy.gui.Button;
import edu.maping.easy.gui.ButtonList;
import edu.maping.easy.gui.GUIEvent;
import edu.maping.easy.gui.GuiFactory;
import edu.maping.easy.gui.ScreenLogger;
import edu.maping.easy.gui.Slider;
import edu.maping.easy.model.GUIInteraction;
import edu.maping.easy.model.Thing;
import edu.maping.easy.sequence.ticker.PropagationTicker;
import edu.maping.easy.sequence.ticker.TickerListener;

public class PropagationManager implements GUIInteraction {

	private GuiFactory guiFactory;
	private ScreenLogger screenLogger;
	private EasyMappingEditor easyMappingEditor;

	Button cancelButton;
	Button okButton;
	Slider delaySlider = null;
	ButtonList buttonList;

	boolean doing = false;

	private List<Thing> propagationThings;

	SequenceStep thingActionEvent = null;
	ActionEvent currentActionEvent;

	private long delayValue = 1;

	public void setup() {
		cancelButton = guiFactory.newButton("Cancel", 0, 0, 40, 20);
		okButton = guiFactory.newButton("Ok", 25, 0, 40, 20);
		buttonList = guiFactory.newButtonList("Propagation", 0, 0, 30,
				10);

		delaySlider = guiFactory.newSlider("Delay", 50, 15, delayValue, 300, 1);

		buttonList.add(cancelButton);
		buttonList.add(okButton);
		buttonList.add(delaySlider);
		buttonList.setPosition(-100, 0);
		buttonList.setDrawable(false);
	}

	public void showform(int x, int y) {
		buttonList.setPosition(x, y);
		buttonList.setDrawable(true);
		doing = true;
	}

	@Override
	public void controlEvent(GUIEvent theEvent) {

		if (doing) {
			if (theEvent.controller() == okButton) {
				propagationThings = easyMappingEditor.getSelectedThings();

				if (propagationThings != null && !propagationThings.isEmpty()) {

					for (int indexThing = 0; indexThing < propagationThings
							.size(); indexThing++) {

						Thing key = propagationThings.get(indexThing);
						ActionEventList actionEventList = thingActionEvent.actionEventMap
								.get(key);

						for (int index = 0; index < actionEventList.size(); index++) {
							ActionEvent actionEvent = actionEventList
									.get(index);

							// BUSCO CANDIDATO PARA ESTABLECER LA PROPAGACION
							if (actionEvent.painterContainer instanceof TickerListener) {
								PropagationTicker propagationTicker = new PropagationTicker();
								actionEvent.ticker = propagationTicker;
								propagationTicker.setDelay(delayValue);
								currentActionEvent.propagationRequired
										.add(propagationTicker);
							}
						}
					}

					// TODO SI NO EXISTE NADIE PARA PROPAGAR DEJAR SELECCIONAR
					// UNO

					buttonList.setDrawable(false);
					doing = false;

				} else {
					screenLogger.writeText("Select Shapes, else pulse cancel");
				}

			} else if (theEvent.controller() == cancelButton) {
				buttonList.setDrawable(false);
				doing = false;
			} else if (theEvent.controller() == delaySlider) {
				delayValue = (long) theEvent.slider.value;
			}
		}

	}

	public void setGuiFactory(GuiFactory guiFactory) {
		this.guiFactory = guiFactory;
	}

	public void setScreenLogger(ScreenLogger screenLogger) {
		this.screenLogger = screenLogger;
	}

	public void setEasyMappingEditor(EasyMappingEditor easyMappingEditor) {
		this.easyMappingEditor = easyMappingEditor;
	}

	public SequenceStep getThingActionEvent() {
		return thingActionEvent;
	}

	public void setThingActionEvent(SequenceStep thingActionEvent) {
		this.thingActionEvent = thingActionEvent;
	}

	public void setCurrentActionEvent(ActionEvent currentActionEvent) {
		this.currentActionEvent = currentActionEvent;
	}

}
